<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class CHttpUtil{
									
	public static function  postDataBySocket($host, $filename, $method, $params, $port = 80, $timeout = 30) {

		if (!$host || !$filename || !$method || !$params)    return false;

				$fp    = fsockopen($host, $port, $errno, $errstr, $timeout);
		if (!$fp)    return false;

		$str = "";
		foreach ($params as $key => $val) {
			$str .= $key. "=". $val. "&";
		}
		$str    = substr($str, 0, -1);
		$method    = strtoupper($method);
		$out = "{$method} {$filename} HTTP/1.1\r\n";
		$out .= "Host: {$host}\r\n";
		$out .= "Content-type: application/x-www-form-urlencoded\r\n";
		$out .= "Content-Length: ". strlen($str) . "\r\n";
		$out .= "User-Agent: MSIE\r\n";
		$out .= "Connection: Close\r\n\r\n";
		$out .= $str."\r\n\r\n";

		fwrite($fp, $out);

				$headers = "";
		while ($str = trim(fgets($fp, 4096)))
		$headers .= "$str/n";

				$body = "";
		while (!feof($fp))
		$body .= fgets($fp, 4096);

		fclose($fp);
		return $body;
	}
	
	public static function existRemoteFile($url){
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		$code = curl_exec($ch);
		$headers = curl_getinfo($ch);
		curl_close($ch);

		if ($headers['http_code'] != 200) {
			return false;
		}
		return true;
	}
	 
	
	public static function getBaseUrl4Yii($request){
		$retVal = $request->hostInfo.$request->baseUrl;
		return $retVal;
	}
}